/**
 * 
 */
package com.shine.icis.dao.imp;

import org.hibernate.Query;

import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.ISubmitLogDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dao.param.SubmitLogQueryParameters;
import com.shine.icis.hbean.SubmitLog;

/**
 * @author zsy
 *
 */
public class SubmitLogDAO extends BaseHibernateDAO implements ISubmitLogDAO {
	
	@Override
	public SubmitLog findById(String id) {
		SubmitLog sl = (SubmitLog)getSession().get(SubmitLog.class, id);
		return sl;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ListPage<SubmitLog> findSubmitLogPage(SubmitLogQueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException("非法参数:查询参数对象为null");
		}
		try {
			StringBuffer sbQueryString = new StringBuffer();			
			sbQueryString.append("select sl from SubmitLog as sl where 1=1");
			if(qp.getBeginDataDate() != null){
				sbQueryString.append(" and sl.dataDate>=:beginDataDate");
			}
			
			if(qp.getEndDataDate() != null){
				sbQueryString.append(" and sl.dataDate<=:endDataDate");
			}
			
			return new CommQuery<SubmitLog>().queryListPage(qp, 
					qp.appendOrders(sbQueryString, "sl" ), getSession());
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<SubmitLog>();
		}
	}
	
	@Override
	public String findInternalVersion(String dataDate) {
		Query query = getSession().createQuery("select max(sl.internalVersion) from SubmitLog as sl where sl.dataDate=:dataDate");
		query.setString("dataDate", dataDate);
		return (String) query.uniqueResult();
	}
	
	@Override
	public int findSubmitLogCount(String dataDate) {
		Query query = getSession().createQuery("select count(sl) from SubmitLog as sl where sl.dataDate=:dataDate");
		query.setString("dataDate", dataDate);
		Long count = (Long)query.uniqueResult();
		return count.intValue();
	}
	
	@Override
	public SubmitLog findLast() {
		Query query = getSession().createQuery("select sl from SubmitLog as sl order by sl.operateTime desc");
		query.setMaxResults(1);
		return (SubmitLog) query.uniqueResult();
	}
}
